/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.yxw.yxnet_cd_center.customer_service.utils;

import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.util.UUID;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
/**
 *
 * @author Administrator
 */
public class ErrorLog 
{
    public static void Log(String filePath, String content) 
    { 
        content = content==null?"":content;
        if(content.equals(""))
        {
           return;
        }        
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdir();
                //f.createNewFile();// 不存在则创建   
            }            
            File f = new File(filePath + java.io.File.separator + ConfigInfor.getErrorLogFileName());  
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }  
            FileOutputStream oFileOutputStream = new FileOutputStream(f,true); 
            content += System.getProperty("line.separator");
            oFileOutputStream.write(content.getBytes());
            oFileOutputStream.close();
        }
        catch(Exception e) 
        {
             Log(e.getMessage());
        }
    }   
    public static void Log(String content) 
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }
        //String filePath = System.getProperty("user.dir") + java.io.File.separator + BaseClass.ConfigInfor.appName + java.io.File.separator + "errorlog";
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator +  BaseDateTime.getShortDateStringNoSlipt();//ConfigInfor.getErrorlogPath();
        //java.io.File.
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + ConfigInfor.getErrorLogFileName()); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }  
            FileOutputStream oFileOutputStream = new FileOutputStream(f,true); 
            content += System.getProperty("line.separator");
            oFileOutputStream.write(content.getBytes());
            oFileOutputStream.close();
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
    }
    public static void LogLoseFile(String content) 
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator + ConfigInfor.getAppName() + java.io.File.separator + "LoseFileLog";
        //String filePath = ConfigInfor.getErrorlogPath();
        //java.io.File.
        FileOutputStream oFileOutputStream = null;
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + "LoseFiles"); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }  
            oFileOutputStream = new FileOutputStream(f,true); 
            content += System.getProperty("line.separator");
            oFileOutputStream.write(content.getBytes());            
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        finally 
        { 
            if (oFileOutputStream != null) 
            {
                try 
                {
                    oFileOutputStream.close();  
                } 
                catch (java.io.IOException e1)
                { 
                    Log(e1.getMessage());
                } 
            } 
        }
    } 
    public static void LogSuccessLoseFile(String content) 
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator + ConfigInfor.getAppName() + java.io.File.separator + "SuccessLoseFileLog";
        //String filePath = ConfigInfor.getErrorlogPath();
        //java.io.File.
        FileOutputStream oFileOutputStream = null;
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + "LoseFiles"); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }  
            oFileOutputStream = new FileOutputStream(f,true); 
            content += System.getProperty("line.separator");
            oFileOutputStream.write(content.getBytes());            
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        finally 
        { 
            if (oFileOutputStream != null) 
            {
                try 
                {
                    oFileOutputStream.close();  
                } 
                catch (java.io.IOException e1)
                { 
                    Log(e1.getMessage());
                } 
            } 
        }
    }
    public static void LogLoseOrder(String orderId,Long memberId) 
    {
        JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("orderId", orderId);
        oJSONObject.put("memberId", memberId);
        String content = oJSONObject.toJSONString() + "\r\n----------------------------------------------------";
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator + ConfigInfor.getAppName() + java.io.File.separator + "LoseOrderLog";
        //String filePath = ConfigInfor.getErrorlogPath();
        //java.io.File.
        FileOutputStream oFileOutputStream = null;
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + "LoseFiles"); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }  
            oFileOutputStream = new FileOutputStream(f,true); 
            content += System.getProperty("line.separator");
            oFileOutputStream.write(content.getBytes());            
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        finally 
        { 
            if (oFileOutputStream != null) 
            {
                try 
                {
                    oFileOutputStream.close();  
                } 
                catch (java.io.IOException e1)
                { 
                    Log(e1.getMessage());
                } 
            } 
        }
    }      
    public static void DeleteFile(String filePath) 
    {   
        try
        {
           File f = new File(filePath);  
           if (f.exists()) 
            { 
                f.delete();
            }  
        }
        catch(Exception e) 
        {
            Log(e.getMessage());
        }
    } 
    public static void Exist(String Path)
    {
        File dir = new File(Path);  
        if (!dir.exists()) 
        { 
            dir.mkdirs();
        } 
    }
    public static String GetTxtFileContent(String FilePath)
    {
        File f = new File(FilePath);  
        if (!f.exists()) 
        { 
            return null;
        }  
        if (!f.isFile()) 
        { 
            return null;
        }  
        StringBuilder oBuilder = new StringBuilder();
        BufferedReader reader = null; 
        try 
        {
            //System.out.println("以行为单位读取文件内容，一次读一行");
            reader = new java.io.BufferedReader(new FileReader(FilePath)); 
            String tempString = null; 
            //int line = 1; 
            while ((tempString = reader.readLine()) != null)
            {
               oBuilder.append(tempString);
            }
            reader.close();
        }
        catch (java.io.IOException e) 
        {  
            Log(e.getMessage());
        } 
        finally 
        { 
            if (reader != null) 
            {
                try 
                {
                    reader.close();  
                } 
                catch (java.io.IOException e1)
                { 
                    Log(e1.getMessage());
                } 
            } 
        }
        return oBuilder.toString();       
    }
    public static void writeTextFile(String content)
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator + ConfigInfor.getAppName();//ConfigInfor.getErrorlogPath(); 
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + UUID.randomUUID().toString().replaceAll("-", "") + ".txt"); 
            if (!f.exists()) 
            { 
               f.createNewFile();// 不存在则创建   
            }  
           
            //StringBuilder oStringBuilder = new StringBuilder();           
            //oStringBuilder.append(BaseDateTime.getLongDateTimeString());
            //oStringBuilder.append("  ");
            //oStringBuilder.append(content);
            //oStringBuilder.append(System.getProperty("line.separator"));
            BufferedWriter bw = null;  
            try 
            {  
                bw = new BufferedWriter(new FileWriter(f));  
                bw.write(content); 
                bw.flush();//强制输出缓冲区的内容，避免数据缓存，造成文件写入不完整的情况。 
            }  
            catch (IOException e)  
            {  
                throw new Exception("目标文件未找到", e);  
            }  
            finally 
            {   //资源关闭  
                if (bw != null)  
                {  
                    try 
                    {  
                       bw.close();  
                    }  
                    catch (IOException e)  
                    {  
                       e.printStackTrace();  
                    }  
                }  
            }            
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }       
        
    }  
    public static void writeTextFile(String fileName,String content)
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }  
        String filePath = ConfigInfor.getAppRootPath() + java.io.File.separator + "RecievData";
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            }
            File f = new File(filePath + java.io.File.separator + fileName); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }
            //StringBuilder oStringBuilder = new StringBuilder();           
            //oStringBuilder.append(BaseDateTime.getLongDateTimeString());
            //oStringBuilder.append("  ");
            //oStringBuilder.append(content);
            //oStringBuilder.append(System.getProperty("line.separator"));
            BufferedWriter bw = null;  
            try 
            {  
               bw = new BufferedWriter(new FileWriter(f));  
               bw.write(content); 
               bw.flush();//强制输出缓冲区的内容，避免数据缓存，造成文件写入不完整的情况。 
            }  
            catch (IOException e)  
            {  
               throw new Exception("目标文件未找到", e);  
            }  
            finally 
            {    //资源关闭  
                if (bw != null)  
                {  
                    try 
                    {  
                       bw.close();  
                    }  
                    catch (IOException e)  
                    {
                       e.printStackTrace(); 
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }    
    public static void writeTextFile(String filePath,String fileName,String content)
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }        
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + fileName); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }
            //StringBuilder oStringBuilder = new StringBuilder();           
            //oStringBuilder.append(BaseDateTime.getLongDateTimeString());
            //oStringBuilder.append("  ");
            //oStringBuilder.append(content);
            //oStringBuilder.append(System.getProperty("line.separator"));
            BufferedWriter bw = null;  
            try 
            {  
               bw = new BufferedWriter(new FileWriter(f));  
               bw.write(content); 
               bw.flush();//强制输出缓冲区的内容，避免数据缓存，造成文件写入不完整的情况。 
            }  
            catch (IOException e)  
            {  
               throw new Exception("目标文件未找到", e);  
            }  
            finally 
            {    //资源关闭  
                if (bw != null)  
                {  
                    try 
                    {  
                       bw.close();  
                    }  
                    catch (IOException e)  
                    {
                       e.printStackTrace(); 
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void WriteUTF8File(String FilePath,String FileName,String Content)
    {
        Content = Content==null?"":Content;
        if(Content.equals(""))
        {
            return;
        }        
        try
        {
            File Dir = new File(FilePath);  
            if (!Dir.exists()) 
            { 
                Dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File oFile = new File(FilePath + java.io.File.separator + FileName); 
            if (!oFile.exists()) 
            { 
                oFile.createNewFile();// 不存在则创建   
            }
            OutputStream oOutputStream = null;
            BufferedWriter oBufferedWriter = null;
            try 
            {
                oOutputStream = new FileOutputStream(oFile);
                oBufferedWriter = new BufferedWriter(new OutputStreamWriter(oOutputStream, "utf-8"));
                oBufferedWriter.write(Content);
                oBufferedWriter.flush();
            } 
            catch(FileNotFoundException e) 
            {
                e.printStackTrace();
            } 
            catch(UnsupportedEncodingException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try 
                {
                    oOutputStream.close();                    
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
                try 
                {
                    oBufferedWriter.close();                    
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }                
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }       
    public static void writeAndMoveTextFile(String FilePath,String FileName,String content,String TargetFilePath,String TargetFileName)
    {
        content = content==null?"":content;
        if(content.equals(""))
        {
            return;
        }
        File dir = null;
        File f = null;
        try
        {
            dir = new File(FilePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            dir = new File(TargetFilePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
            }        
            f = new File(FilePath + java.io.File.separator + FileName); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }
            //StringBuilder oStringBuilder = new StringBuilder();           
            //oStringBuilder.append(BaseDateTime.getLongDateTimeString());
            //oStringBuilder.append("  ");
            //oStringBuilder.append(content);
            //oStringBuilder.append(System.getProperty("line.separator"));
            BufferedWriter bw = null;  
            try 
            {  
               bw = new BufferedWriter(new FileWriter(f));  
               bw.write(content); 
               bw.flush();//强制输出缓冲区的内容，避免数据缓存，造成文件写入不完整的情况。 
            }  
            catch (IOException e)  
            {  
               throw new Exception("目标文件未找到", e);  
            }  
            finally 
            {    //资源关闭  
                if (bw != null)  
                {  
                    try 
                    {  
                       bw.close();  
                    }  
                    catch (IOException e)  
                    {
                       e.printStackTrace(); 
                    }
                }                
            }
            File TargetFile = new File(TargetFilePath + java.io.File.separator + TargetFileName);        
            f.renameTo(TargetFile);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }       
    public static void writeImageFile(String filePath,String fileName,byte[] content)
    {
        if(null == content)
        {
            return;
        }        
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            BufferedImage bufferedImage = null;
            ByteArrayInputStream byteArrInput = new ByteArrayInputStream(content);
            try 
            {
                bufferedImage = ImageIO.read(byteArrInput);
                if(bufferedImage == null) 
                {
                   return;
                }
                ImageIO.write(bufferedImage, "jpg", new File(filePath + java.io.File.separator + fileName));	
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
    public static void writeFile(byte[] content)
    {
        String filePath = ConfigInfor.getAppRootPath();//System.getProperty("user.dir");
        String fileName = java.util.UUID.randomUUID().toString().replaceAll("-", "") + ".data";//ConfigInfor.ErrorLogFileName;
        writeFile(filePath,fileName,content);
    }    
    public static void writeFile(String filePath,byte[] content)
    {
        String fileName = java.util.UUID.randomUUID().toString().replaceAll("-", "") + ".data";//ConfigInfor.ErrorLogFileName;
        writeFile(filePath,fileName,content);
    }
    public static void writeFile(String filePath,String fileName,byte[] content)
    {
        if(null == content)
        {
            return;
        }        
        try
        {
            File dir = new File(filePath);  
            if (!dir.exists()) 
            { 
                dir.mkdirs();
                //f.createNewFile();// 不存在则创建   
            } 
            File f = new File(filePath + java.io.File.separator + fileName); 
            if (!f.exists()) 
            { 
                f.createNewFile();// 不存在则创建   
            }
            DataOutputStream oDataOutputStream = null;
            try 
            {
                oDataOutputStream = new DataOutputStream(new FileOutputStream(filePath + java.io.File.separator + fileName, true));
                oDataOutputStream.write(content);
            } 
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            } 
            finally 
            {
                oDataOutputStream.close();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }    
}